import { Component } from '@angular/core';
import { NavController, IonicPage, ViewController } from 'ionic-angular';
import { AppGlobal } from "../../app/app.global";
import { TranslateService } from '@ngx-translate/core';
import { Menu } from '../../entity/Menu';
import { ItemService } from "../../provider/ItemService";
import { AlertsProvider } from "../../providers/alerts/alerts";

import { HTTP } from '@ionic-native/http';
import { LocateProvider } from '../../providers/locate/locate';
import { ToastsProvider } from '../../providers/toasts/toasts';

import { ModalController } from 'ionic-angular';
import { DetailsPage } from '../details/details';

declare var window;
@IonicPage()
@Component({
  selector: 'page-home',
  templateUrl: 'home.html'
})
export class HomePage {

  user: any;
  location: any;
  menus: any;
  items: any;//菜单列表

  msgList: Array<any> = [];
  customeTags:Array<any> = ["default"];

  constructor(public navCtrl: NavController,
    public translate: TranslateService,
    public http: HTTP,
    public alerts: AlertsProvider,
    public locate: LocateProvider,
    public toastsCtrl: ToastsProvider,
    public modals: ModalController) {

    this.user = AppGlobal.user;//得到当前用户
    this.location = this.locate.getCurrentLocation();//得到当前位置
    this.menus = AppGlobal.getMenus();
    this.getNearbyHouses();
    // this.items = ItemService.getIndexItems();//得到推荐
  }


  /**
   * 得到5公里以内的房源信息
   * @returns {any} 200 执行成功 500服务器错误 404 未知错误
   */
  getNearbyHouses(): any {
    var serve = AppGlobal.getServe();//得到服务器地址
    this.http.get(serve + "v1/common/houses", {}, {})
      .then(response => {
        var data = JSON.parse(response.data);
        if (data.status == 200) {
          this.items = data.data;
          return 200;
        }
        return 500;//返回500错误
      })
      .catch(error => {
        // this.alerts.simpleAlert("读取数据错误");
        return 404;//返回404错误
      })
    return 404;
  }

  /**
   * 下拉刷新 重新加载附近房源
   * 5秒延迟加载失败
   * 加载成功给出toast 提示
   * @param refresher
   */
  doRefresh(refresher) {
    var reloadSuccessText = this.translate.get('RELOADSUCCESS');
    setTimeout(() => {
      var s = this.getNearbyHouses()
      if (s == 200) {/*todo 加载成功给用户提示*/
        refresher.complete();
      }
      this.toastsCtrl.alert("更新成功，找到3套新房源" + s);
      refresher.complete();
    }, 5000);
  }

  /**
   * 转到详情页面
   */
  toDetails(item) {
    this.navCtrl.push("DetailsPage", { item: item })
    // let schoolModal = this.modals.create("DetailsPage").present();
    // console.log(schoolModal)
  }

  // todo测试
  testToDetailsPage() {
    this.navCtrl.push("DetailsPage", { item: "" })
  }

  ionViewDidLoad() {
    //开启极光推送
    if (window.plugins && window.plugins.jPushPlugin) {
      console.log("开启极光推送")
      window.JPush.setTags({ sequence: 1, tags: this.customeTags },
        (result) => {
          var sequence = result.sequence
          var tags = result.tags  // 数组类型
        }, (error) => {
          var sequence = error.sequence
          var errorCode = error.code
        });

        document.addEventListener("jpush.receiveMessage", function (event) {
          var message
          
        }, false)
    }
  }


}
